
const { ccclass, property } = cc._decorator;
import baseManager from "../../../app/base/baseManager";
import { app } from "../../../app/app";
@ccclass
export default class recorderManager extends baseManager {
    private time = 0;
    private videoUrl = '';
    private isrecoding = false;

    private startCB: Function = null;
    private pauseCB: Function = null;
    private resumeCB: Function = null;
    private stopCB: Function = null;
    private errorCB: Function = null;

    public getVideoUrl() {
        return this.videoUrl;
    }

    public getVideoTime() {
        return this.time;
    }

    public begin(startCB?: Function) {
        this.startCB = startCB;

        this.time = 0;
        this.videoUrl = '';
        this.isrecoding = false;

        app.lib.sdk.recorder.start({
            onStart: () => {
                this.isrecoding = true;
                if (this.startCB) {
                    this.startCB();
                    this.startCB = null;
                }
            },
            onPause: () => {
                this.isrecoding = false;
                if (this.pauseCB) {
                    this.pauseCB();
                    this.pauseCB = null;
                }
            },
            onResume: () => {
                this.isrecoding = true;
                if (this.resumeCB) {
                    this.resumeCB();
                    this.resumeCB = null;
                }
            },
            onStop: (url) => {
                this.isrecoding = false;
                this.videoUrl = url || '';
                if (this.stopCB) {
                    this.stopCB(this.videoUrl, this.time);
                    this.stopCB = null;
                }
            },
            onError: () => {
                this.isrecoding = false;
                if (this.errorCB) {
                    this.errorCB();
                    this.errorCB = null;
                }
            }
        });
    }

    public pause(pauseCB?: Function) {
        this.pauseCB = pauseCB;
        app.lib.sdk.recorder.pause();
    }

    public resume(resumeCB?: Function) {
        this.resumeCB = resumeCB;
        app.lib.sdk.recorder.resume();
    }

    public stop(stopCB?: (url: string, time: number) => void) {
        this.stopCB = stopCB;
        app.lib.sdk.recorder.stop();
    }

    public update(dt: number) {
        if (this.isrecoding) {
            this.time += dt;
        }
    }
};